package pl.ptemich.eav.engine.physics;

import pl.ptemich.eav.engine.physics.impl.CircleParameters;
import pl.ptemich.eav.engine.physics.impl.RectangleParameters;


public class ColissionAlgorithmStrategy {

  public static boolean checkCollision(StaticObject shape1, StaticObject shape2) {
    switch (shape1.getShapeParameters().getShapeType()) {
      case RECTANGLE:
        switch (shape2.getShapeParameters().getShapeType()) {
          case RECTANGLE:
            return checkRectangleToRectangleCollision(shape1, shape2);
          case CIRCLE:
            return checkCircleToRectangleCollision(shape2, shape1);
        }
        break;
      case CIRCLE:
        switch (shape2.getShapeParameters().getShapeType()) {
          case RECTANGLE:
            return checkCircleToRectangleCollision(shape1, shape2);
          case CIRCLE:
            return checkCircleToCircleCollision(shape1, shape2);
        }        
        break;
    }
    return false;
  }
  
  private static boolean checkCircleToRectangleCollision(StaticObject shape1, StaticObject shape2) {
    CircleParameters circleParams  = (CircleParameters) shape1.getShapeParameters();
    RectangleParameters rectParams  = (RectangleParameters) shape2.getShapeParameters();
    if ((shape1.getWorldPosition().getPosX() + circleParams.getRadius() > (shape2.getWorldPosition().getPosX() - rectParams.getWidth()/2)) //from the left 
        && (shape1.getWorldPosition().getPosX() - circleParams.getRadius() < (shape2.getWorldPosition().getPosX() + rectParams.getWidth()/2)) //from the right
        && (shape1.getWorldPosition().getPosY() + circleParams.getRadius() > (shape2.getWorldPosition().getPosY() - rectParams.getHeight()/2)) //from the bottom
        && (shape1.getWorldPosition().getPosY() - circleParams.getRadius() < (shape2.getWorldPosition().getPosY() + rectParams.getHeight()/2))) //from the top
    {
      return true;
    }
    
    return false;
  }
  
  private static boolean checkCircleToCircleCollision(StaticObject shape1, StaticObject shape2) {
    CircleParameters circleParams1  = (CircleParameters) shape1.getShapeParameters();
    CircleParameters circleParams2  = (CircleParameters) shape2.getShapeParameters();
    if ((circleParams1.getRadius() + circleParams2.getRadius()) > Math.sqrt(((shape1.getWorldPosition().getPosX() - shape2.getWorldPosition().getPosX()) * (shape1.getWorldPosition().getPosX() - shape2.getWorldPosition().getPosX())) + ((shape1.getWorldPosition().getPosY() - shape2.getWorldPosition().getPosY()) * (shape1.getWorldPosition().getPosY() - shape2.getWorldPosition().getPosY())))) {
      return true;
    }
      
    return false;
  }   
  
  public static boolean checkRectangleToRectangleCollision(StaticObject shape1, StaticObject shape2) {    
    RectangleParameters rectParams1  = (RectangleParameters) shape1.getShapeParameters();
    RectangleParameters rectParams2  = (RectangleParameters) shape2.getShapeParameters();
    if ((shape1.getWorldPosition().getPosX() + rectParams1.getWidth() / 2) < (shape2.getWorldPosition().getPosX() - rectParams2.getWidth() / 2)) {
      return false;
    } else if ((shape1.getWorldPosition().getPosX() - rectParams1.getWidth() / 2) > (shape2.getWorldPosition().getPosX() + rectParams2.getWidth() / 2)) {
      return false;
    } else if ((shape1.getWorldPosition().getPosY() + rectParams1.getHeight() / 2) < (shape2.getWorldPosition().getPosY() - rectParams2.getHeight() / 2)) {
      return false;
    } else if ((shape1.getWorldPosition().getPosY() - rectParams1.getHeight() / 2) > (shape2.getWorldPosition().getPosY() + rectParams2.getHeight() / 2)) {
      return false;
    } 
        
    return true;
  }
  
}
